package cn.myzqu.kafka.demo.producer2;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.serialization.StringSerializer;

import java.util.Properties;

/**
 * @author guoqiujing
 * @date 2020/11/25 11:34
 */
public class ProducerFastStart {


    private static final String brokerList = "172.168.50.171:9092";
    private static final String topic = "topic-demo";


    private static Properties initConfig() {
        Properties properties = new Properties();
        properties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
        properties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
        properties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, brokerList);
        return properties;
    }

    public static void main(String[] args) {
        Properties properties = initConfig();
        // 配置生成者客户端参数并创建KafkaProducer实例
        KafkaProducer producer = new KafkaProducer(properties);
        // 构建需要发送的消息
        ProducerRecord record = new ProducerRecord(topic, "hello,Kafka!");
        // 发送消息
        boolean flag = true;
        try {
            while (flag) {
                producer.send(record);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            producer.close();
        }
    }


}
